package org.mybatis.generator.my.plugins;

import org.apache.commons.lang3.StringUtils;
import org.mybatis.generator.api.IntrospectedColumn;
import org.mybatis.generator.api.IntrospectedTable;
import org.mybatis.generator.api.PluginAdapter;
import org.mybatis.generator.api.dom.java.Field;
import org.mybatis.generator.api.dom.java.FullyQualifiedJavaType;
import org.mybatis.generator.api.dom.java.Method;
import org.mybatis.generator.api.dom.java.TopLevelClass;

import java.util.List;


/**
 * @author wangfupeng
 */
public class MybatisPluginComment extends PluginAdapter {


    private FullyQualifiedJavaType apiModel;
    private FullyQualifiedJavaType apiModelProperty;
    private boolean addGWTInterface;
    private boolean suppressJavaInterface;
    private FullyQualifiedJavaType data;
    private FullyQualifiedJavaType noArgsConstructor;
    private FullyQualifiedJavaType builder;
    private FullyQualifiedJavaType allArgsConstructor;

    public MybatisPluginComment() {
        super();
//        apiModel = new FullyQualifiedJavaType("io.swagger.annotations.ApiModel");
//        apiModelProperty = new FullyQualifiedJavaType("io.swagger.annotations.ApiModelProperty");
//        allArgsConstructor = new FullyQualifiedJavaType("lombok.AllArgsConstructor");
//        builder = new FullyQualifiedJavaType("lombok.Builder");
//        data = new FullyQualifiedJavaType("lombok.Data");
//        noArgsConstructor = new FullyQualifiedJavaType("lombok.NoArgsConstructor");
    }

    @Override
    public boolean validate(List<String> warnings) {
        return true;
    }

    @Override
    public boolean modelBaseRecordClassGenerated(TopLevelClass topLevelClass,
                                                 IntrospectedTable introspectedTable) {
        topLevelClass.getJavaDocLines().clear();
//        topLevelClass.addImportedType(apiModel);
//        topLevelClass.addImportedType(apiModelProperty);
//        topLevelClass.addImportedType(data);
//        topLevelClass.addImportedType(builder);
//        topLevelClass.addImportedType(allArgsConstructor);
//        topLevelClass.addImportedType(noArgsConstructor);
//        topLevelClass.addAnnotation("@ApiModel(\"" + introspectedTable.getRemarks() + "\")");
//        topLevelClass.addAnnotation("@Data");
//        topLevelClass.addAnnotation("@Builder");
//        topLevelClass.addAnnotation("@NoArgsConstructor");
//        topLevelClass.addAnnotation("@AllArgsConstructor");
        String comment = StringUtils.isEmpty(introspectedTable.getRemarks()) ?
                introspectedTable.getFullyQualifiedTable().toString() : introspectedTable.getRemarks();
        topLevelClass.addJavaDocLine("/**");
        topLevelClass.addJavaDocLine(" * Comment:    " + comment);
        topLevelClass.addJavaDocLine(" * Table:      " + introspectedTable.getFullyQualifiedTable());
        topLevelClass.addJavaDocLine(" **/");
        return true;
    }



    @Override
    public boolean modelFieldGenerated(Field field,
                                       TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn,
                                       IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        field.getJavaDocLines().clear();
        String comment = StringUtils.isEmpty(introspectedColumn.getRemarks()) ?
                introspectedColumn.getActualColumnName() : introspectedColumn.getRemarks();
        field.addJavaDocLine("/**");
        field.addJavaDocLine(" * Comment:    " + comment);
        field.addJavaDocLine(" * Column:     " + introspectedColumn.getActualColumnName());
        field.addJavaDocLine(" * Nullable:   " + introspectedColumn.isNullable());
        field.addJavaDocLine(" */");
//        System.out.println(introspectedColumn.getActualColumnName() + " = " +introspectedColumn.getRemarks());
//        field.addAnnotation("@ApiModelProperty(\"" + introspectedColumn.getRemarks().replaceAll("\"", "“") + "\")");
        return true;
    }

    @Override
    public boolean modelGetterMethodGenerated(Method method,
                                              TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn,
                                              IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        method.getJavaDocLines().clear();
        return true;
    }

    @Override
    public boolean modelSetterMethodGenerated(Method method,
                                              TopLevelClass topLevelClass, IntrospectedColumn introspectedColumn,
                                              IntrospectedTable introspectedTable, ModelClassType modelClassType) {
        method.getJavaDocLines().clear();
        return true;
    }


}
